/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.widget.item.impl;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.constants.client.i18n.Messages;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.oracle.SimpleSuggestion;
import com.genia.toolbox.web.gwt.basics.client.rpc.AbstractAsynCallback;
import com.genia.toolbox.web.gwt.form.client.form.DynamicListBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.ValuedItemEntry;
import com.genia.toolbox.web.gwt.form.client.form.impl.ValuedItemEntryImpl;
import com.genia.toolbox.web.gwt.form.client.process.ProcessForm;
import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;
import com.google.gwt.user.client.ui.SuggestOracle.Request;

/**
 * the implementation of <code>SimpleItemWidget</code> for a
 * <code>ListBoxItem</code>.
 */
public class DynamicListBoxItemWidget
    extends AbstractListBoxItemWidget<DynamicListBoxItem>
{

  /**
   * constructor.
   * 
   * @param formIdentifier
   *          the identifier of the form containing this widget
   * @param dataIdentifier
   *          the data identifier
   * @param item
   *          the item this object represents.
   */
  public DynamicListBoxItemWidget(final String formIdentifier, final DataIdentifier dataIdentifier, final DynamicListBoxItem item)
  {
    super(item);
    ProcessForm.getFormService().getSuggestions(formIdentifier, dataIdentifier, item.getName(), new Request(null, -1), new AbstractAsynCallback<List<SimpleSuggestion>>()
    {

      public void onSuccess(final List<SimpleSuggestion> suggestions)
      {
        if (suggestions == null) {
          setValuedItemEntrys(item, new ArrayList<ValuedItemEntry>());
          return;
        }
        List<ValuedItemEntry> valuedItemEntrys = new ArrayList<ValuedItemEntry>();
        for (final SimpleSuggestion simpleSuggestion : suggestions) {
          ValuedItemEntryImpl vie = new ValuedItemEntryImpl();
          vie.setLabel(new GwtI18nMessage(Messages.SHOW_PARAMETER_KEY, simpleSuggestion.getDisplayString()));
          vie.setValue(simpleSuggestion.getValue());
          valuedItemEntrys.add(vie);
        }
        setValuedItemEntrys(item, valuedItemEntrys);
      }

    });
  }

}
